
package com.yili.oa.service.impl;

import com.yili.oa.dao.UserMapper;
import com.yili.oa.entity.UserSearch;
import com.yili.oa.models.Admin;
import com.yili.oa.models.User;
import com.yili.oa.models.UserExample;
import com.yili.oa.service.UserService;
import com.yili.oa.utils.Exception.AjaxException;
import com.yili.oa.utils.ListUtils;
import com.yili.oa.utils.annotate.DataBase;
import com.yili.oa.vo.request.UserDataReq;
import com.yili.oa.vo.response.UserDataRes;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;


/**
 * @author lxr
 * @version v0.01
 * @date 2019/3/21 0021
 * @email 200890949@qq.com
 */

@SuppressWarnings("ALL")
@Service
@DataBase("lyt_game_db")
@Transactional(value = "transactionManager")
public class UserSvImpl implements UserService {


    @Autowired
    private UserMapper mapper;

    @Override
    public List<User> getAllUser() {
        UserExample example = new UserExample();
//        example.setOrderByClause("user_sort asc,user_update_time desc");
        return mapper.selectByExample(example);
    }

    @Override
    public Boolean add(UserDataReq userDataReq) {
        return true;
    }

    @Override
    public Boolean checkAccount(String account,String userId) {
//        UserExample example = new UserExample();
//        UserExample.Criteria criteria = example.createCriteria();
//        criteria.andUserAccountEqualTo(account);
//        if (userId!=null) {
//            criteria.andUserIdNotEqualTo(userId);
//        }
//        List<User> list = mapper.selectByExample(example);
//        if(ListUtils.isNotNullOrEmpty(list)){
//            return false;
//        }
        return true;
    }

    @Override
    public Boolean update(UserDataReq userDataReq) {
//        User createUser = (User) SecurityUtils.getSubject().getSession().getAttribute("user");
//        User user = new User();
//        BeanUtils.copyProperties(userDataReq,user);
//        user.setUserUpdateTime(new Date());
//        user.setUserUpdateUser(createUser.getUserId());
//        try {
//            mapper.updateByPrimaryKeySelective(user);
//        }catch (Exception e){
//            return false;
//        }
        return true;
    }

    @Override
    public User getById(Integer userId){
        User user = mapper.selectByPrimaryKey(userId);
        return user;
    }

    @Override
    public List<User> searchUser(UserSearch search, Admin admin) {
        UserExample example = new UserExample();
        UserExample.Criteria criteria = example.createCriteria();
        //代理只能获取自己底下的游戏玩家
        if (admin.getIsAdmin().equals(0)){
            criteria.andAgentIdEqualTo(admin.getUsername());
        }
        if (StringUtils.isNotBlank(search.getId())){
            criteria.andUseridEqualTo(Integer.valueOf(search.getId()));
        }
        if (StringUtils.isNotBlank(search.getUserStatus())){
            criteria.andEnableEqualTo(Byte.valueOf(search.getUserStatus()));
        }
//        if (StringUtils.isNotBlank(search.getUserStatus())){
//            criteria.andUserStatusEqualTo(search.getUserStatus());
//        }
        example.setOrderByClause("create_time DESC");
        return mapper.selectByExample(example);
    }

    @Override
    public Boolean del(List<String> userIds) throws AjaxException {

//        UserExample example = new UserExample();
//        UserExample.Criteria criteria = example.createCriteria();
//        criteria.andUserIdIn(userIds);
//        try {
//            mapper.deleteByExample(example);
//        }catch (Exception e){
//            return false;
//        }
        return true;
    }

    @Override
    public Boolean updStatus(UserDataReq userDataReq) {
        User user = new User();
        user.setUserid(Integer.valueOf(userDataReq.getUserId()));
        user.setEnable(Byte.valueOf(userDataReq.getUserStatus()));
        try {
            mapper.updateByPrimaryKeySelective(user);
        }catch (Exception e){
            return false;
        }
        return true;
    }

    @Override
    public Boolean updRole(String id) {
//        User user = mapper.selectByPrimaryKey(id);
//        if (user==null){
//            throw new AjaxException("用户不存在");
//        }
//        user.setUserRole(null);
//        try {
//            mapper.updateByPrimaryKey(user);
//        }catch (Exception e){
//            return false;
//        }
        return true;
    }

    @Override
    public User getByAccount(Integer userid) {
        UserExample example = new UserExample();
        UserExample.Criteria criteria = example.createCriteria();
        criteria.andUseridEqualTo(Integer.valueOf(userid));
        List<User> users = mapper.selectByExample(example);
        User user = new User();
        if (ListUtils.isNotNullOrEmpty(users)){
            user=users.get(0);
        }
        return user;
    }

    @Override
    @Cacheable(value = "userCheck",key="#token")
    public User getByToken(String token) {
        UserExample example = new UserExample();
        UserExample.Criteria criteria = example.createCriteria();
        criteria.andTokenEqualTo(token);
        List<User> list = mapper.selectByExample(example);
        User user = null;
        if (ListUtils.isNotNullOrEmpty(list)){
            user = list.get(0);
        }
        return user;
    }

    @Override
    public void addCoins(Integer userId, Long coins) {
        User user = mapper.selectByPrimaryKey(userId);
        if (user==null){
            throw new AjaxException("用户不存在");
        }
        BigDecimal sum = new BigDecimal(user.getCoins());
        sum.add(new BigDecimal(coins));
        user.setCoins(sum.longValue());
        try {
            mapper.updateByPrimaryKeySelective(user);
        }catch (Exception e){
            throw new AjaxException("未知错误 请重试");
        }
    }
}

